package com.example.combwyaq.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.combwyaq.R;

public class BottomButton extends RelativeLayout implements View.OnClickListener {

    private int nomalImg=0;//默认未选中图片id
    private int selImg =0;//选中图片id
    private int nomalColor = 0;//默认文字颜色
    private int selColor = 0;//选中文字颜色
    private String content;//设置底部显示文字
    private boolean selFlag = false;//当前控件是否被选中标志
    private LinearLayout linearLayout;
    private ImageView img;
    private TextView contentTv;
    private TextView numTv;
    private SelectListener listener;
    private String TAG="YQS";
    private Paint redPaint;
    private boolean showPoint = false;//是否显示小红点标志
    private boolean isShow = false;//是否显示 默认隐藏


    public BottomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.bottom_arrs);
        selFlag=typedArray.getBoolean(R.styleable.bottom_arrs_selflag,false);
        typedArray.recycle();//释放TypedArrat对象->内存泄漏
        init();

    }


    public BottomButton setListener(SelectListener listener) {
        this.listener = listener;
        return this;
    }

    public BottomButton setContent(String content) {
        this.content = content;
        if (this.content.isEmpty())
            return this;

        Log.i(TAG, "setContent: "+content);
        contentTv.setText(content);
        return this;
    }
    private void init(){
        Log.i(TAG, "init: ");
        linearLayout=new LinearLayout(getContext());
        img=new ImageView(getContext());
        contentTv=new TextView(getContext());
        numTv=new TextView(getContext());
        //先组合LinearLayout 下的ImagerView 以及TextView
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams imgLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        img.setLayoutParams(imgLp);
        LinearLayout.LayoutParams contentLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        contentTv.setLayoutParams(contentLp);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.addView(img);
        linearLayout.addView(contentTv);
        RelativeLayout.LayoutParams linearLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        linearLp.addRule(RelativeLayout.CENTER_IN_PARENT);
        linearLayout.setLayoutParams(linearLp);
        addView(linearLayout);
        RelativeLayout.LayoutParams  numLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        numLp.rightMargin= ConvertUtils.dp2px(15);
        numLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        numTv.setLayoutParams(numLp);
        addView(numTv);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        setOnClickListener(this::onClick);


    }







    public BottomButton setNomalImg(int nomalImg) {
        this.nomalImg = nomalImg;
        Log.i(TAG, "setNomalImg: ");
        if (!selFlag)
            img.setImageResource(this.nomalImg);
        return this;
    }

    public BottomButton setSelImg(int selImg) {
        this.selImg = selImg;
        Log.i(TAG, "setSelImg: "+selImg);
        if (selFlag)
            img.setImageResource(this.selImg);
        return this;
    }

    public BottomButton setNomalColor(int nomalColor) {
        this.nomalColor = nomalColor;
        Log.i(TAG, "setNomalColor: "+nomalColor);
        if (!selFlag)
            contentTv.setTextColor(this.nomalColor);
        return this;
    }

    public BottomButton setSelColor(int selColor) {
        this.selColor = selColor;
        Log.i(TAG, "setSelColor: "+selColor);
        contentTv.setTextColor(this.selColor);
        return this;
    }

    public BottomButton setShowPoint(boolean showPoint) {
        this.showPoint = showPoint;
        Log.i(TAG, "setShowPoint: ");
        if (this.showPoint)
            numTv.setVisibility(VISIBLE);
        else
            numTv.setVisibility(GONE);
        return this;
    }

    @Override
    public void onClick(View v) {
        Log.i(TAG, "onClick: ");
        selFlag=true;
    refreshSelect();
    if (listener!=null){
        listener.onSelect(getId());
    }

    }

    public void clearSelect(){
        Log.i(TAG, "clearSelect: ");
        selFlag=false;
        img.setImageResource(nomalImg);
        contentTv.setTextColor(nomalColor);

    }

    private void refreshSelect(){
        if (selFlag){
            Log.i(TAG, "refreshSelect: ");
            img.setImageResource(selImg);
            contentTv.setTextColor(selColor);
            numTv.setVisibility(GONE);
        }
    }
    //选中回调接口
    public interface SelectListener{
        void onSelect(int id);
    }

}
